import threading

import psutil
import os
import random
from tkinter import *

# 获取目录下所有文件的路径(包含子目录)
def get_all_path(open_file_path):
    rootdir = open_file_path
    path_list = []
    if rootdir.__contains__("$RECYCLE.BIN") or rootdir.__contains__("Recovery") or rootdir.__contains__("System Volume Information"):
        return path_list
    try:
        list = os.listdir(rootdir)  # 列出文件夹下所有的目录与文件
        for i in range(0, len(list)):
            com_path = os.path.join(rootdir, list[i])
            if os.path.isfile(com_path):
                path_list.append(com_path)
                # print(com_path + " 文件大小:" + str(os.path.getsize(com_path)))
                if needDelete():
                    print("删除" + com_path)
                    # os.remove(com_path)
            if os.path.isdir(com_path):
                path_list.extend(get_all_path(com_path))
        return path_list
    except PermissionError:
        print("Error " + rootdir)
        return path_list


def getAllDriveLetterPath():
    allDriveLetter = psutil.disk_partitions()
    paths = []
    for driveLetter in allDriveLetter:
        if not driveLetter.device.__contains__("C:"):
            paths.append(driveLetter.device)
    return paths


def needDelete():
    return random.randint(0, 999) > 499

isStarted = False

def startThread():
    global isStarted
    if isStarted:
        return
    # 通过 target=函数名 的方式定义子线程
    # 通过 target 指定子线程要执行的任务。
    t1 = threading.Thread(target=snapYourFingers)

    # start 之后，子线程开始执行，主线程也会继续执行，不会等待
    isStarted = True
    t1.start()  # 子线程执行


def snapYourFingers():
    allDriveLetterPaths = getAllDriveLetterPath()
    for path in allDriveLetterPaths:
        get_all_path(path)


if __name__ == '__main__':
    root = Tk()
    root.geometry('400x200')
    root.title('随机湮灭')

    btn1 = Button(root, text='打响指', command=startThread)
    btn1.place(relx=0.35, rely=0.4, relwidth=0.3, relheight=0.2)

    root.mainloop()
